#include "BLDC_voltage_condition.h"
extern pole_status pole_status_set;
u32 voltage_adc_8in1[3], current_adc_8in1[3];
u32 voltage_adc_2in1[3], voltage_adc_2in1_old[3], current_adc_2in1[3];
// u32 voltage_adc_1in1[3], voltage_adc_1in1_old[3], current_adc_1in1[3];
u32 voltage_adc_average_8in1[3], voltage_adc_average_8in1_sum[3], voltage_adc_average_8in1_average[3], voltage_adc_average_2in1_average[3], voltage_adc_average_1in1_average[3];
int32_t current_adc_average_8in1[3], current_adc_average_8in1_sum[3], current_adc_average_8in1_average[3], current_adc_average_2in1_average[3], current_adc_average_1in1_average[3];

uint32_t voltage_rising_sum_for_test[3], voltage_down_sum_for_test[3];
char i_pole = 0, i_average_test = 0, i_check[3], reading_average_now = 0;
u16 overflow_check_flag = 100, test_number_flag = 0;

u16 *p_voltage_adc_1in1_u, *p_voltage_adc_1in1_v, *p_voltage_adc_1in1_w, *p_current_adc_1in1_u, *p_current_adc_1in1_v, *p_current_adc_1in1_w;
u16 voltage_adc_1in1[3][6][64], current_adc_1in1[3][6][64];
void adc_input_gets_and_eval(u16 voltage_u, u16 voltage_v, u16 voltage_w, u16 current_u, u16 current_v, u16 current_w)
{

    //获取平均值
    if (reading_average_now)
    {
        voltage_adc_8in1[0] = ((voltage_adc_8in1[0] * 7) >> 3) + voltage_u;
        voltage_adc_8in1[1] = ((voltage_adc_8in1[1] * 7) >> 3) + voltage_v;
        voltage_adc_8in1[2] = ((voltage_adc_8in1[2] * 7) >> 3) + voltage_w;

        current_adc_8in1[0] = ((current_adc_8in1[0] * 7) >> 3) + current_u;
        current_adc_8in1[1] = ((current_adc_8in1[1] * 7) >> 3) + current_v;
        current_adc_8in1[2] = ((current_adc_8in1[2] * 7) >> 3) + current_w;
    }

    //低速低通滤波
    // voltage_adc_2in1[0] = (voltage_adc_2in1[0] >> 1) + voltage_u;
    // voltage_adc_2in1[1] = (voltage_adc_2in1[1] >> 1) + voltage_v;
    // voltage_adc_2in1[2] = (voltage_adc_2in1[2] >> 1) + voltage_w;

    // current_adc_2in1[0] = (current_adc_2in1[0] >> 1) + current_u;
    // current_adc_2in1[1] = (current_adc_2in1[1] >> 1) + current_v;
    // current_adc_2in1[2] = (current_adc_2in1[2] >> 1) + current_w;
    //快速赋值添加

    if (overflow_check_flag < 64)
    {
        *p_voltage_adc_1in1_u = voltage_u;
        *p_voltage_adc_1in1_v = voltage_v;
        *p_voltage_adc_1in1_w = voltage_w;
        *p_current_adc_1in1_u = current_u;
        // *p_current_adc_1in1_v = current_v;
        *p_current_adc_1in1_w = current_w;
        p_voltage_adc_1in1_u++;
        p_voltage_adc_1in1_v++; //
        p_voltage_adc_1in1_w++;
        p_current_adc_1in1_u++; //
                                // p_current_adc_1in1_v++;
        p_current_adc_1in1_w++;
        overflow_check_flag++;
    }
}
void set_voltage_and_current_average(void)
{
    i_average_test++;
    if (i_average_test < 4)
    {
        voltage_adc_average_8in1_sum[0] = 0;
        voltage_adc_average_8in1_sum[1] = 0;
        voltage_adc_average_8in1_sum[2] = 0;
        current_adc_average_8in1_sum[0] = 0;
        current_adc_average_8in1_sum[1] = 0;
        current_adc_average_8in1_sum[2] = 0;
    }
    else if (i_average_test <= 20 && i_average_test > 4)
    {
        voltage_adc_average_8in1_sum[0] += voltage_adc_8in1[0];
        voltage_adc_average_8in1_sum[1] += voltage_adc_8in1[1];
        voltage_adc_average_8in1_sum[2] += voltage_adc_8in1[2];

        current_adc_average_8in1_sum[0] += current_adc_8in1[0];
        current_adc_average_8in1_sum[1] += current_adc_8in1[1];
        current_adc_average_8in1_sum[2] += current_adc_8in1[2];
        /* code */
    }
    else if (i_average_test == 22)
    {
        //八次滤波计算
        voltage_adc_average_8in1_average[0] = voltage_adc_average_8in1_sum[0] >> 4;
        voltage_adc_average_8in1_average[1] = voltage_adc_average_8in1_sum[1] >> 4;
        voltage_adc_average_8in1_average[2] = voltage_adc_average_8in1_sum[2] >> 4;

        current_adc_average_8in1_average[0] = current_adc_average_8in1_sum[0] >> 4;
        current_adc_average_8in1_average[1] = current_adc_average_8in1_sum[1] >> 4;
        current_adc_average_8in1_average[2] = current_adc_average_8in1_sum[2] >> 4;

        // 2次滤波计算
        voltage_adc_average_2in1_average[0] = voltage_adc_average_8in1_sum[0] >> 6;
        voltage_adc_average_2in1_average[1] = voltage_adc_average_8in1_sum[1] >> 6;
        voltage_adc_average_2in1_average[2] = voltage_adc_average_8in1_sum[2] >> 6;

        current_adc_average_2in1_average[0] = current_adc_average_8in1_sum[0] >> 6;
        current_adc_average_2in1_average[1] = current_adc_average_8in1_sum[1] >> 6;
        current_adc_average_2in1_average[2] = current_adc_average_8in1_sum[2] >> 6;

        // 1次平均计算
        voltage_adc_average_1in1_average[0] = voltage_adc_average_8in1_sum[0] >> 7;
        voltage_adc_average_1in1_average[1] = voltage_adc_average_8in1_sum[1] >> 7;
        voltage_adc_average_1in1_average[2] = voltage_adc_average_8in1_sum[2] >> 7;

        current_adc_average_1in1_average[0] = current_adc_average_8in1_sum[0] >> 7;
        current_adc_average_1in1_average[1] = current_adc_average_8in1_sum[1] >> 7;
        current_adc_average_1in1_average[2] = current_adc_average_8in1_sum[2] >> 7;
    }
    else if (i_average_test == 24)
    {
        i_average_test--;
    }
}

u32 get_adc_pole_voltage_value(char pole_number)
{
    return voltage_adc_2in1[pole_number];
}
int get_adc_pole_current_value(char pole_number)
{
    return current_adc_8in1[pole_number] - current_adc_average_8in1_average[pole_number];
}
int get_adc_pole_current_2in1_value(char pole_number)
{
    return current_adc_2in1[pole_number] - current_adc_average_2in1_average[pole_number];
}
void change_over_zero_param(void)
{
    p_voltage_adc_1in1_u = voltage_adc_1in1[0][pole_status_set.u_pole_status];
    p_voltage_adc_1in1_v = voltage_adc_1in1[1][pole_status_set.v_pole_status];
    p_voltage_adc_1in1_w = voltage_adc_1in1[2][pole_status_set.w_pole_status];
    p_current_adc_1in1_u = current_adc_1in1[0][pole_status_set.u_pole_status];
    p_current_adc_1in1_v = current_adc_1in1[1][pole_status_set.v_pole_status];
    p_current_adc_1in1_w = current_adc_1in1[2][pole_status_set.w_pole_status];
    overflow_check_flag = 0;
}
//上升下降检测，已经被前后差值法替代
//注意，次数过少的循环使用循环会大大增加计算量
